#include "LPC11xx.h"			/* LPC11xx Peripheral Registers */
#include "../Common/type.h"
#include "../DaisyLink/DaisyLink.h"
#include "../Main/DL40.h"
#include "timer.h"

BYTE g_timer16_0_type = 0;
BYTE g_timer16_1_type = 0;
BYTE g_timer32_0_type = 0;
BYTE g_timer32_1_type = 0;

void TIMER16_0_IRQHandler(void)
{	
	if (LPC_TMR16B0->IR & 0x1) // Match Channel 0
	{
		LPC_TMR16B0->IR = 0x1; // Clear interrupt flag
		if (g_timer16_0_type == CONFIGPIN_CAPTURE)
		{			
			g_pinCounters[2].Count += (1ul << 0x10);
		}
	}
}

void TIMER16_1_IRQHandler(void)
{
	if (LPC_TMR16B1->IR & 0x1) // Match Channel 0
	{
		LPC_TMR16B1->IR = 0x1; // Clear interrupt flag
		if (g_timer16_1_type == CONFIGPIN_CAPTURE)
		{			
			g_pinCounters[20].Count += (1ul << 0x10);
		}
	}
}

void TIMER32_0_IRQHandler(void)
{
	if (LPC_TMR32B0->IR & 0x1) // Match Channel 0
	{
		LPC_TMR32B0->IR = 0x1; // Clear interrupt flag
		if (g_timer32_0_type == CONFIGPIN_CAPTURE)
		{			
			g_pinCounters[17].Count += (1ull << 0x20);
		}
	}
}

void TIMER32_1_IRQHandler(void)
{
	if (LPC_TMR32B1->IR & 0x1) // Match Channel 0
	{
		LPC_TMR32B1->IR = 0x1; // Clear interrupt flag
		if (g_timer32_0_type == CONFIGPIN_CAPTURE)
		{			
			g_pinCounters[12].Count += (1ull << 0x20);
		}
	}
}

void TIMERInit(void)
{
	NVIC_EnableIRQ(TIMER_16_0_IRQn);
  NVIC_EnableIRQ(TIMER_16_1_IRQn);
  NVIC_EnableIRQ(TIMER_32_0_IRQn);
  NVIC_EnableIRQ(TIMER_32_1_IRQn);
}
